package com.alibaba.otter.manager.biz.config.record.dal;

import com.alibaba.otter.manager.biz.config.record.dal.dataobject.LogRecordDO;
import com.alibaba.otter.manager.biz.config.record.dto.LogRecordQueryDTO;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * LogRecordDao: 数据操作接口
 * <p>
 * 这只是一个减少手工创建的模板文件
 * 可以任意添加方法和实现, 更改作者和重定义类名
 * <p/>@author Powered By Fluent Mybatis
 */
public interface LogRecordDao extends BaseMapper<LogRecordDO> {
    List<LogRecordDO> listByPipelineId(Long pipelineId);

    List<LogRecordDO> listByPipelineIdWithoutContent(Long pipelineId);

    int getCount(Map<String,Object> condition);

    default Page<LogRecordDO> pageLogs(LogRecordQueryDTO queryDTO, Pageable pageable){
        QueryWrapper q = QueryWrapper.create()
                .eq(LogRecordDO::getPipelineId, queryDTO.getPipelineId(), Objects::nonNull)
                .eq(LogRecordDO::getTitle, queryDTO.getMonitorName(), StringUtils::isNotBlank)
                .like(LogRecordDO::getMessage,"replace(" + queryDTO.getSearchKey() + ",'_','\\_')",StringUtils.isNotBlank(queryDTO.getSearchKey()))
                .orderBy(LogRecordDO::getGmtCreate,false);
        return paginate(pageable.getPageNumber(), pageable.getPageSize(), q);
    }
}
